package com.exp.controller;

import com.common.controller.BaseController;
import com.exp.model.Business;
import com.exp.model.HotBusiness;
import com.exp.service.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * Created by guojianyu on 2016/3/26.
 */
@Controller
@RequestMapping("/business")
public class BusinessController extends BaseController {
    @Autowired
    private BusinessService businessService;
    /*我的商家页面*/
    @RequestMapping("/view")
    public String view() {
        return "business/businessListManage";
    }
    /*管理员查看商家页面*/
    @RequestMapping("/viewAll")
    public String viewAll() {
        return "business/businessList";
    }
    /*管理员获取商家All列表*/
    @RequestMapping("/listAll")
    @ResponseBody
    public List<Business> list(@RequestParam Map map) {
        List<Business> businesses = businessService.getListAll(map);
        return businesses;
    }
   /*获取列表*/
    @RequestMapping("/list")
    @ResponseBody
    public List<Business> list(@RequestParam Map map,HttpSession session) {
        String businessId= String.valueOf(getLoginBusinessId(session));
        map.put("businessId",businessId);
        List<Business> businesses = businessService.getList(map);
        return businesses;
    }
    /*注册商家*/
    @RequestMapping("/businessRegister")
    @ResponseBody
    public boolean register(@RequestBody Business business) {
        business.setStatus("disabled");
       int flag=businessService.add(business);
       if(flag>0){
           return true;
       }else {
           return  false;
       }
    }
    /*修改状态 begin*/
    @RequestMapping("/enabled")
    @ResponseBody
    /*不加@RequestBody 是因为在index引入defaultHeader*/
    public boolean enabled(Business business) {
        if(business.getId()!=null){
            business.setStatus("enabled");
            businessService.enabled(business);
        }
        return true;
    }
    @RequestMapping("/disabled")
    @ResponseBody
    public boolean disabled( Business business) {
        if(business.getId()!=null){
            business.setStatus("disabled");
            businessService.disabled(business);
        }
        return true;
    }
    /*修改状态 end*/
    /*修改*/
    @RequestMapping("/update")
    @ResponseBody
    public boolean update(Business business){
        if (business.getId() != null) {
            business.setStatus("disabled");
            businessService.update(business);
        }
        return true;
    }
}
